package com.sky.wsp.warning.log;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import cn.hutool.http.HttpUtil;


/**
 * 语音告警
 *
 * @author 24044
 **/
public class LogAudioWarnAppender extends ConsoleAppender<LoggingEvent> {
    private boolean enable = true;
    private String url = null;

    @Override
    protected void subAppend(LoggingEvent event) {
        if (null == url) {
            init();
        }
        if (!enable) {
            return;
        }
        String msg = event.getFormattedMessage();
        try {
            HttpUtil.get(url + msg);
        } catch (Exception e) {

        }
    }

    private void init() {
        String warnUrl = this.getContext().getProperty("warnUrl");
        String warnEnv = this.getContext().getProperty("warnEnv");
        String applicationName = this.getContext().getProperty("applicationName");
        if (null == warnUrl || null == warnEnv || null == applicationName) {
            enable = false;
            return;
        }
        String audio = applicationName + "-" + warnEnv;
        url = warnUrl + "?audio=" + audio + "&content=";
    }
}